//-----------------------------------------------------------------------
// <copyright file="CertCheckMode.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Shaun Wilde (ICS)</author>
// <email>v-swilde</email>
// <date>2006-04-03</date>
// <summary>TODO</summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.Web.WebSite
{
    using System;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Enable or disable CRL checking on a website
    /// </summary>
    public class CertCheckMode : TaskBase
    {
        /// <summary>
        /// The description of the web site to locate
        /// </summary>
        private string description;

        /// <summary>
        /// The identifier of thw website to locate
        /// </summary>
        private string identifier;

        /// <summary>
        /// enable the CRL
        /// </summary>
        private bool enable;

        /// <summary>
        /// the original CRL value
        /// </summary>
        private bool originalEnable;

        private string machineName = "localhost";

        /// <summary>
        /// Machine to operate on
        /// </summary>
        public string MachineName
        {
            get { return this.machineName; }
            set { this.machineName = value; }
        }

        /// <summary>
        /// Enable the CRL - sets CertCheckMode to 0
        /// </summary>
        public bool Enable
        {
            get { return this.enable; }

            set { this.enable = value; }
        }

        /// <summary>
        /// Enable the CRL - sets CertCheckMode to 0
        /// </summary>
        [Output]
        public bool OriginalEnable
        {
            get { return this.originalEnable; }

            set { this.originalEnable = value; }
        }

        /// <summary>
        /// Description of web site to modify
        /// </summary>
        /// <value>A valid web site description</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Identifier">Identifier</seealso> 
        /// for the site you wish to start</remarks>
        public string Description
        {
            get { return (this.description ?? String.Empty); }

            set { this.description = value; }
        }

        /// <summary>
        /// web site idenfitier to modify
        /// </summary>
        /// <value>A valid web site identifier</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Description">Description</seealso> 
        /// for the site you wish to start</remarks>
        public string Identifier
        {
            get { return (this.identifier ?? String.Empty); }

            set { this.identifier = value; }
        }

        /// <summary>
        /// Set the CertCheckMode mode property
        /// </summary>
        protected override void InternalExecute()
        {
            int id = Utilities.CheckWebSiteDescriptionOrIdentifier(this.description, this.identifier, "CertCheckMode", this.MachineName);

            if (Microsoft.Sdc.Tasks.Configuration.Web.WebSite.Exists(this.MachineName, id))
            {
                Microsoft.Sdc.Tasks.Configuration.Web.WebSite web = Microsoft.Sdc.Tasks.Configuration.Web.WebSite.Load(this.MachineName, id);
                this.originalEnable = (web.CertCheckMode(this.enable ? 0 : 1)) == 0 ? true : false;
            }
        }
    }
}